package com.woniuxy.service.impl;

import com.woniuxy.dao.PrescriptionDao;
import com.woniuxy.entity.*;
import com.woniuxy.service.PrescriptionService;
import com.woniuxy.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/*
* 使用全局异常处理
* */
@Slf4j
@Service
public class PrescriptionServiceImpl implements PrescriptionService {
    @Autowired
    PrescriptionDao prescriptionDao;

    @Override
    public Integer updateStatusByPrescriptionId(String prescriptionNo, Integer prescriptionStatus) {
        return prescriptionDao.updateStatusByPrescriptionId(prescriptionNo,prescriptionStatus);
    }

    @Override
    public Integer selectPreAndPatientCount(Integer prescriptionStatus) {
        return prescriptionDao.selectPreAndPatientCount(prescriptionStatus);
    }

    @Override
    public String addPrescription(Prescription prescription) {


        Prescription findPrescription = prescriptionDao.selectByRegisterId(prescription.getRegisterId());
        if(findPrescription!=null){
            return findPrescription.getPrescriptionNo();
        }
        String uuid = CommonUtil.getUuid();
        prescription.setPrescriptionNo(uuid);
        prescription.setPrescriptionTime(new Date());
        prescriptionDao.insertPrescription(prescription);
        log.info("完成insertPrescription");
        return uuid;
    }

    @Override
    public List<PrescriptionAndPatient> selectPreAndPatient(Integer pageIndex, Integer pageSize, Integer prescriptionStatus) {
        return prescriptionDao.selectPreAndPatient(pageIndex,pageSize,prescriptionStatus);
    }

    @Override
    public PageBean<Prescription> getPrescriptionCondition(Prescription prescription, int pageIndex, int pageSize) {

        List<Prescription> prescriptions =
                prescriptionDao.selectPrescriptionByCondition(prescription,pageIndex,pageSize);
        //计算所有的项目总价
        BigDecimal totalPrice=new BigDecimal(0);
        for(Prescription findPrescription : prescriptions){
                for (DrugItem drugItems : findPrescription.getDrugItems()){
                    totalPrice=drugItems.getDrugItemTotalPrice().add(totalPrice);
            }
            for (InspectItem inspectItems : findPrescription.getInspectItems()){
                totalPrice=inspectItems.getInspectItemTotalPrice().add(totalPrice);
            }
            findPrescription.setTotalprice(totalPrice);
            totalPrice =new BigDecimal(0).multiply(totalPrice);
        }

        PageBean<Prescription> pageBean = new PageBean<>();
        pageBean.setBeanList(prescriptions);
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        int totalRecord = prescriptionDao.getPrescriptionNum(prescription);
        pageBean.setTotalRecord(totalRecord);
        return pageBean;
    }

    @Override
    public void modifyPrescriptionStatus( String prescriptionNO) {
        prescriptionDao.updateStatusByPrescriptionNo(prescriptionNO);
    }

    @Override
    public void modifyConditionDetails(String prescriptionNo, String conditionDetails) {
        prescriptionDao.updateConditionDetailsByPrescriptionNo(prescriptionNo,conditionDetails);
    }
}
